package com.cloudlead.web.resource;

import com.cloudlead.common.lang.Messages;
import com.cloudlead.common.lang.api.ErrorActionResponse;
import com.cloudlead.common.lang.exception.RequestNotAcceptableException;
import com.cloudlead.common.lang.utils.ExceptionHelper;
import com.cloudlead.plugin.security.service.CodeService;
import com.cloudlead.plugin.security.utils.VerifyCodeUtils;
import org.apache.commons.lang3.StringUtils;

import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.*;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

@Path("/auth")
@Produces(MediaType.APPLICATION_JSON)
public class AuthorzationController {

    @Inject
    private CodeService codeService;

    @Context
    HttpServletRequest request;

    @Context
    HttpServletResponse response;

    @GET
    @Path("code.jpg")
    public Response verifyCode(@QueryParam("requestId") String requestId) {
        try {
            if (StringUtils.isBlank(requestId)) {
                throw new RequestNotAcceptableException(Messages.getMessage("security.authorization.request.id.required"));
            }
            if (StringUtils.isNotBlank(codeService.getCode(requestId))) {
                throw new RequestNotAcceptableException(Messages.getMessage("security.authorization.request.id.repeat"));
            }
            response.setHeader("Pragma", "No-cache");
            response.setHeader("Cache-Control", "no-cache");
            response.setDateHeader("Expires", 0);
            response.setContentType("image/jpeg");

            //生成随机字串
            String verifyCode = VerifyCodeUtils.generateVerifyCode(4);
            //存入缓存
            codeService.addCode(requestId, verifyCode);
            //生成图片
            int w = 200, h = 80;
            VerifyCodeUtils.outputImage(w, h, response.getOutputStream(), verifyCode);
            return Response.ok().build();
        } catch (Exception e) {
            e.printStackTrace();
            ErrorActionResponse response = ExceptionHelper.toResponse(e);
            throw new WebApplicationException(e.getMessage(), e, response.getCode());
        }
    }


    @GET
    @Path("code.jpg/check")
    public Response checkCode(@QueryParam("requestId") String requestId, @QueryParam("code") String code) {
        try {
            if (StringUtils.isBlank(requestId)) {
                throw new RequestNotAcceptableException(Messages.getMessage("security.authorization.request.id.required"));
            }
            if (StringUtils.isBlank(code)) {
                throw new RequestNotAcceptableException(Messages.getMessage("security.authorization.code.NotNull"));
            }
            String verify = codeService.getCode(requestId);
            boolean pass = true;
            if (!code.equalsIgnoreCase(verify)) {
                pass = false;
            }
            return Response.ok(pass).build();
        } catch (Exception e) {
            e.printStackTrace();
            ErrorActionResponse response = ExceptionHelper.toResponse(e);
            throw new WebApplicationException(e.getMessage(), e, response.getCode());
        }
    }

}
